package com.qzdsoft.erpcloud.controller.act;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.controller.stock.StockPuOutBoundController;
import com.qzdsoft.erpcloud.domain.act.CenterMsg;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.service.act.CenterMsgService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.query.act.CenterMsgInfo;
import com.qzdsoft.erpcloud.vo.query.act.CenterMsgQueryParam;
//import com.qzd.erpcloud.util.SessionUtil;
import com.qzdsoft.utils.SysUtils;
import com.qzdsoft.vo.EnumPo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.act.ActCenterMsgStatusEnum;
import com.qzdsoft.vo.act.ActCenterMsgTypeEnum;

/**
 * Created by Yang
 */
@Controller
@RequestMapping("/act/center/msg")
public class CenterMsgController extends BaseController{
    
    private static final Logger logger = LoggerFactory.getLogger(CenterMsgController.class);

//    @Autowired
//    private RedisHelper redisHelper;
    @Autowired
    CenterCompanyService actCenterCompanyService;

    @Autowired
    private CenterMsgService actCenterMsgService;

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        
        List<EnumPo> typeList = new ArrayList<EnumPo>();
        Map<Object, Object> typeMap = new HashMap<Object, Object>();
        for(ActCenterMsgTypeEnum e : ActCenterMsgTypeEnum.values()){
            SysUtils.enums(typeList, typeMap, e.getKey(), e.getVal());
        }

        List<EnumPo> statusList = new ArrayList<EnumPo>();
        Map<Object, Object> statusMap = new HashMap<Object, Object>();
        for(ActCenterMsgStatusEnum e : ActCenterMsgStatusEnum.values()){
            SysUtils.enums(statusList, statusMap, e.getKey(), e.getVal());
        }
        
        mv.addObject("typeList", typeList);
        mv.addObject("typeMap", typeMap);

        mv.addObject("statusList", statusList);
        mv.addObject("statusMap", statusMap);

//        mv.addObject("actCenterCompanyMap", redisHelper.getActCenterCompanyMap());
        mv.addObject("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
        mv.setViewName("act/center_msg/index");
        return mv;
    }

    /**
     *
     * @param request
     * @param param
     * @return
     */
    @ResponseBody
    @RequestMapping("/data")
    public LayTableResponse<CenterMsgInfo> data(HttpServletRequest request, CenterMsgQueryParam param) {
//        Integer companyId = SessionUtil.getLoggedCompanyId(request);
        Integer companyId = param.getCompanyId();
        if(null != companyId){
            param.setCompanyId(companyId);
        }

        return actCenterMsgService.selectData(param.getCompanyId(), param.getTitle(), param.getType(), param.getStatus(), param.getTimeFrom(), param.getTimeTo(), param.getPage(), param.getLimit());
    }

    /**
     * 编辑数据
     * @param request
     * @return
     */
    @ResponseBody
    @Permission(validate=false)
    @RequestMapping("/edit")
    public ModelAndView edit(Integer companyId,Integer id) {
        ModelAndView mv = new ModelAndView();
        List<EnumPo> typeList = new ArrayList<EnumPo>();
        Map<Object, Object> typeMap = new HashMap<Object, Object>();
        for(ActCenterMsgTypeEnum e : ActCenterMsgTypeEnum.values()){
            SysUtils.enums(typeList, typeMap, e.getKey(), e.getVal());
        }

        List<EnumPo> statusList = new ArrayList<EnumPo>();
        Map<Object, Object> statusMap = new HashMap<Object, Object>();
        for(ActCenterMsgStatusEnum e : ActCenterMsgStatusEnum.values()){
            SysUtils.enums(statusList, statusMap, e.getKey(), e.getVal());
        }
        
        mv.addObject("typeList", typeList);
        mv.addObject("typeMap", typeMap);

        mv.addObject("statusList", statusList);
        mv.addObject("statusMap", statusMap);

//        mv.addObject("actCenterCompanyMap", redisHelper.getActCenterCompanyMap());
        mv.addObject("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
        
        if(null != id){
//            return Response.getSuccess(actCenterMsgService.selectById(id));
        	mv.addObject("centerMsg", actCenterMsgService.selectById(id));
        	mv.setViewName("act/center_msg/edit");
        }else {
            mv.setViewName("act/center_msg/add");
        }
        return mv;
    }

    /**
     *
     * @param request
     * @param msg
     * @return
     */
    @ResponseBody
    @SysLog(description="消息编辑保存")
    @RequestMapping("/edit_execute")
    public Object editExecute(HttpServletRequest request, CenterMsg msg) {
        logger.debug("消息编辑保存："+msg);
//        msg.setCompany_id(SessionUtil.getLoggedCompanyId(request, msg.getCompany_id()));
        return actCenterMsgService.edit(msg);
    }
    /**
     *
     * @param request
     * @param msg
     * @return
     */
    @ResponseBody
    @SysLog(description="消息新增保存")
    @RequestMapping("/create_execute")
    public Object createExecute(HttpServletRequest request, CenterMsg msg) {
        logger.debug("消息新增保存："+msg);
//        msg.setCompany_id(SessionUtil.getLoggedCompanyId(request, msg.getCompany_id()));
        return actCenterMsgService.edit(msg);
    }

    /**
     * 根据主键删除
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @SysLog(description="消息删除")
    @RequestMapping("/del_execute")
    public Response<String> delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
        logger.debug("消息删除id："+id);
        return actCenterMsgService.del(id);
    }
    /**
     * 查看详情
     * @param id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/detail")
    public ModelAndView detail(Integer id) {
        ModelAndView mv = new ModelAndView();
        CenterMsg centerMsg= actCenterMsgService.selectById(id);
        mv.addObject("centerMsg", centerMsg);
        mv.setViewName("act/center_msg/view");
        return mv;
    }

}
